#include <system.h>
#include <drivers/input/mouse.h>

static unsigned int detect_ps2_mouse();
static unsigned int ps2_mouse();

u8 inb(u16 port)
{
    u8 ret;
    __asm__ __volatile__("inb %1, %0" : "=a" (ret) : "dN" (port));
    return ret;
}

static unsigned int ps2_mouse(void)
{
	return 1;
}

static void mouse_wait(unsigned char a_type)
{
  unsigned int _time_out=100000;
  if(a_type==0)
  {
    while(_time_out--) //Data
    {
      if((inb(0x64) & 1)==1)
      {
        return;
      }
    }
    return;
  }
  else
  {
    while(_time_out--) //Signal
    {
      if((inb(0x64) & 2)==0)
      {
        return;
      }
    }
    return;
  }
}

static unsigned char mouse_read()
{
  //Get's response from mouse
  mouse_wait(0);
  return inb(0x60);
}

static unsigned int detect_ps2_mouse(void)
{
    unsigned char tmp = mouse_read();
    if(tmp != 0xFA)
         return 0; //Return no mouse detected
    else
         return 1; //Return mouse detected
}


MouseDriver PS2_driver =
{
	detect_ps2_mouse
};


char Check_PS2M(MouseDriver * driver)
{
	*driver = PS2_driver;
	return (detect_ps2_mouse());
}
